package org.liaohailong.library.widget

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.widget.ImageView
import org.liaohailong.library.R

/**
 * Author: liaohailong
 * Date: 2019/3/26
 * Time: 7:59 PM
 * Description: 自定义圆角图片
 **/
class CornerImageView(context: Context, attributeSet: AttributeSet?, defStyle: Int) : ImageView(context, attributeSet, defStyle) {
    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)
    constructor(context: Context) : this(context, null)

    private var leftTopRadius: Float = 0f
    private var leftBottomRadius: Float = 0f
    private var rightTopRadius: Float = 0f
    private var rightBottomRadius: Float = 0f

    init {
        val array = context.obtainStyledAttributes(attributeSet, R.styleable.CornerImageView)
        leftTopRadius = array.getDimensionPixelSize(R.styleable.CornerImageView_leftTopRadius, 0).toFloat()
        leftBottomRadius = array.getDimensionPixelSize(R.styleable.CornerImageView_leftBottomRadius, 0).toFloat()
        rightTopRadius = array.getDimensionPixelSize(R.styleable.CornerImageView_rightTopRadius, 0).toFloat()
        rightBottomRadius = array.getDimensionPixelSize(R.styleable.CornerImageView_rightBottomRadius, 0).toFloat()
        array.recycle()

        //圆角裁剪必须使用软解模式
        setLayerType(LAYER_TYPE_SOFTWARE, null)
    }

    private val xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
    private lateinit var frameBitmap: Bitmap
    private lateinit var contentBitmap: Bitmap
    private lateinit var contentCanvas: Canvas
    private val contentPaint = Paint().apply {
        color = Color.GREEN
        isAntiAlias = true
    }
    private val framePath = Path()
    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        framePath.reset()
        val rectF = RectF()
        rectF.set(0f, 0f, w.toFloat(), h.toFloat())
        val floatArray = FloatArray(8)
        floatArray[0] = leftTopRadius
        floatArray[1] = leftTopRadius
        floatArray[2] = rightTopRadius
        floatArray[3] = rightTopRadius
        floatArray[4] = rightBottomRadius
        floatArray[5] = rightBottomRadius
        floatArray[6] = leftBottomRadius
        floatArray[7] = leftBottomRadius
        framePath.addRoundRect(rectF, floatArray, Path.Direction.CW)

        frameBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(frameBitmap)
        val paint = Paint()
        paint.isAntiAlias = true
        paint.color = Color.RED
        canvas.drawPath(framePath, paint)

        contentBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
        contentCanvas = Canvas(contentBitmap)
    }

    override fun onDraw(canvas: Canvas?) {
        if (canvas == null) return

        //绘制形状
        contentPaint.xfermode = null
        canvas.drawBitmap(frameBitmap, 0F, 0F, contentPaint)

        //绘制内容
        contentBitmap.eraseColor(Color.TRANSPARENT)
        super.onDraw(contentCanvas)
        contentPaint.xfermode = xfermode
        canvas.drawBitmap(contentBitmap, 0F, 0F, contentPaint)
    }
}